Fix horizontal off-by-one error
authorJonas Bernoulli <jonas@bernoul.li>
Thu, 4 Aug 2022 12:05:09 +0000 (14:05 +0200)
committerJonas Bernoulli <jonas@bernoul.li>
Thu, 4 Aug 2022 12:05:09 +0000 (14:05 +0200)
Delay increasing width used by columns until we know that we have to
do so because we have determined that there is enough room to add an
additional column and a space between the last two columns.

If we don't do that, then we can easily get an off-by-one error.  If
docstrings are shown and the window is narrow, then it is likely that
we end up using the maximal width.  If we then add one to the actual
width and later compare that again with the maximal width, then that
is too width.

which-key.el

index 8162d207f70accc87c74e6a7cdfcf2f9814de5c6..9d5e403f6d85d5d60aa10ea448546446323b0b74 100644 (file)
@@ -1877,7 +1877,7 @@ that width."
                              (which-key--max-len
                                col-keys 2
                               which-key-min-column-description-width)))
-         (col-width      (+ col-key-width col-sep-width col-desc-width))
+         (col-width      (+ col-key-width col-sep-width col-desc-width))
         (col-format     (concat "%" (int-to-string col-key-width)
                                  "s%s%-" (int-to-string col-desc-width) "s")))
     (cons col-width
@@ -1915,10 +1915,10 @@ as well as metadata."
         (while (and cols-w-widths
                     (or (null which-key-max-display-columns)
                         (< n-columns which-key-max-display-columns))
-                    (<= (+ (caar cols-w-widths) page-width) avl-width))
+                    (<= (+ page-width 1 (caar cols-w-widths)) avl-width))
           (setq col (pop cols-w-widths))
           (push (cdr col) page-cols)
-          (cl-incf page-width (car col))
+          (cl-incf page-width (1+ (car col)))
           (cl-incf n-keys (length (cdr col)))
           (cl-incf n-columns))
         (push (which-key--join-columns page-cols) pages)